//
//  ZZCDynamicViewController.m
//  B段项目
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCDynamicViewController.h"
#import "ZZCDetailViewController.h"
#import "ZZCDynamicListCell.h"
@interface ZZCDynamicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain)UICollectionView *myCollectionView;
@property (nonatomic, retain)NSMutableArray *listArray;/**<列表数组*/
@property (nonatomic, assign)BOOL isUpLoad;

@end

@implementation ZZCDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.titleLabel.text = @"碎片";
    
    //图标
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - WIDTH/4.5, WIDTH/40, WIDTH/16, WIDTH/16)];
    img.image = [UIImage imageNamed:@"iconfont-sanyecao.png"];
    img.center = CGPointMake(WIDTH - WIDTH/5, self.navigationController.navigationBar.frame.size.height /2);
    [self.rightView addSubview:img];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item(cell)的大小
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3.2, self.view.frame.size.width / 3.2);
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 2;
    // 设置最小列间距
    flowLayout.minimumInteritemSpacing = 2;
    // 设置item与四周边界的距离  上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.height / 67, self.view.frame.size.width / 42.6, self.view.frame.size.height / 67, self.view.frame.size.width / 42.6);
    
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    
    self.listArray = [NSMutableArray array];
    
    // 注册cell
    [self.myCollectionView registerClass:[ZZCDynamicListCell class] forCellWithReuseIdentifier:@"listCell"];
    
    
    //请求数据
    [self getAFNetWorkingData];
    
    //调用上拉
    [self addHeaderRefresh];
    self.isUpLoad = NO;
    
}


#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    self.isUpLoad = NO;//标记为下拉操作
    // 重新请求数据
    [self getAFNetWorkingData];

}



#pragma mark- 数据解析
- (void)getAFNetWorkingData{
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"正在为您加载.....";
    [self.view addSubview:hub];
    
    NSString *url = @"http://api2.pianke.me/timeline/tags";
    NSString *body = @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=0&version=3.0.6";
    
    [ZYTools postUrl:url body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.listArray removeAllObjects];
        }
        
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *imageArray = [bigDic objectForKey:@"list"];
        
        for (NSDictionary *dic in imageArray) {
            
            ZZCDynamicModel *listModel = [[ZZCDynamicModel alloc] init];
            [listModel setValuesForKeysWithDictionary:dic];
            [self.listArray addObject:listModel];
        }
        
        [self.myCollectionView.mj_header endRefreshing];
        [self.myCollectionView reloadData];
        
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        [hub hide:YES];
    }];
    
    
    
}


// item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArray.count;
}

// section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZZCDynamicListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"listCell" forIndexPath:indexPath];
    ZZCDynamicModel *fragmentModel = [self.listArray objectAtIndex:indexPath.row];
    cell.listModel = fragmentModel;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    return CGSizeMake((screenWidth - 30) / 3, (screenWidth - 30) / 3);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZZCDetailViewController *detailVC = [[ZZCDetailViewController alloc] init];
    ZZCDynamicModel *fragmentList = [self.listArray objectAtIndex:indexPath.item];
    detailVC.nameStr = fragmentList.tag;
    [self.navigationController pushViewController:detailVC animated:YES];
}


    
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
